import { gltfmodeleffectClickCallback } from '@/service';
import { generateListLayerName } from '@/service/layer';
import { useSceneStore } from '@/store/scene';
import { retFilePath } from '@/utils';

export const addGltfModelEffect = (selEffect: any) => {
  const sceneStore = useSceneStore();

  // 模型特效添加
  function addGltfmodel(position: number[]) {
    const { cimInstance } = window;
    const modelJson = {
      enable: true,
      name: '模型特效' + Math.random().toString().split('.')[1],
      type: 'gltfmodel',
      positions: [position],
      dataSourceUrl: retFilePath(selEffect.value!.quoteUrl),
      scale: 1,
      rotation: [0, 0, 0],
      minimumPixelSize: 128,
      rotateZ: true,
      rotateZAngle: 6,
      shaderName: 'unlit',
    };
    const modelAttr = {
      position: modelJson.positions[0],
      translation: [0, 0, 0],
      rotation: modelJson.rotation,
      scale: modelJson.scale,
      color: '',
    };
    const layerParam: API.Scene.ILayer = {
      code: Math.random().toString().split('.')[1],
      show: true,
      json: modelJson,
      name: generateListLayerName('gltfmodel', 'gltfmodeleffect'),
      layerName: modelJson.name,
      desc: '',
      type: 'gltfmodel',
      actualType: 'gltfmodeleffect',
      attr: modelAttr,
    };
    cimInstance.api.closeSceneOffset();
    sceneStore.layerList.unshift(layerParam);
    setTimeout(() => {
      gltfmodeleffectClickCallback({
        code: 'effect',
        clickObject: {
          id: layerParam.layerName,
        },
      });
    }, 0);
  }

  return {
    addGltfmodel,
  };
};
